package model;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;

public class Commande extends AbstractModele implements Serializable
{
	private ArrayList <LigneCommandeRestaurant> lignesCommandeRestaurant;
	private Calendar deliveryDate;
	private int idConfirmation;
	private boolean isReady, isDelivered;
	private String adresse;
	private Client c;
	
	
	public Commande()
	{
		lignesCommandeRestaurant = new ArrayList<LigneCommandeRestaurant>();
		isReady = isDelivered = false;
		deliveryDate = Calendar.getInstance();		
	}
	
	//getters
	public int getIdConfirmation()
	{
		return idConfirmation;
	}
	
	public boolean isReady()
	{
		return isReady;
	}
	
	public boolean isDelivered()
	{
		return isDelivered;
	}
	
	public Calendar getDeliveryDate()
	{
		return deliveryDate;
	}
	
	public Client getClient()
	{
		return c;
	}
	
	public String getAdresse()
	{
		return adresse;
	}
	
	//setters
	public void setClient(Client _c)
	{
		c = _c;
	}
	
	public void setReady(boolean _isReady)
	{
		isReady = _isReady;
		setChanged();
		notifyObservers();
	}
	
	public void setDelivered(boolean _isDelivered)
	{
		isDelivered = _isDelivered;
		setChanged();
		notifyObservers();
	}
	
	public void setIdConfirmation(int _idConfirmation)
	{
		idConfirmation = _idConfirmation;
		setChanged();
		notifyObservers();
	}
	
	public void setAdresse(String _adresse)
	{
		adresse = _adresse;
	}
	
	
	
	public void addLigneCommande(Restaurant restaurant, LigneCommande _ligneCommande)
	{
		boolean found = false;
		for(LigneCommandeRestaurant lcr : lignesCommandeRestaurant)
		{
			if(lcr.getRestaurant().equals(restaurant))
			{
				lcr.addLigneCommande(_ligneCommande);
				found = true;
			}
		}
		
		if(!found)
		{
			LigneCommandeRestaurant lcr = new LigneCommandeRestaurant(restaurant);
			lcr.addLigneCommande(_ligneCommande);
			lignesCommandeRestaurant.add(lcr);
		}
		setChanged();
		notifyObservers();
	}
	
	public void removeLigneCommande(LigneCommande _ligneCommande)
	{
		for(int i=0; i < lignesCommandeRestaurant.size(); i++)
		{
			LigneCommandeRestaurant lcr = lignesCommandeRestaurant.get(i);
			lcr.removeLigneCommande(_ligneCommande);
			if(lcr.getLignesCommande().size()==0) lignesCommandeRestaurant.remove(i); 
		}

		setChanged();
		notifyObservers();
	}
	
	public void clear()
	{
		lignesCommandeRestaurant.clear();		
		
		setChanged();
		notifyObservers();
	}
	
	public ArrayList<LigneCommandeRestaurant> getLignesCommandeRestaurant()
	{
		return lignesCommandeRestaurant;
	}
	
	public float getTotal()
	{
		float prixTotal = 0;
		
		for(LigneCommandeRestaurant lcr : lignesCommandeRestaurant) 
		{
		     prixTotal += lcr.getTotal();
		}
		return prixTotal;		
	}
	
	public void setDeliveryTime(int hour, int min)
	{
		deliveryDate.set(Calendar.HOUR_OF_DAY, hour);//24 format
		deliveryDate.set(Calendar.MINUTE, min);
		setChanged();
		notifyObservers();
	}
	
	@Override
	public String toString()
	{
		return "Commande " + idConfirmation;
	}

	public void refresh() 
	{
		setChanged();
		notifyObservers();
	}
}
